package com.javakc.platform.modules.circle.info.service;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javakc.platform.components.base.service.BaseService;
import com.javakc.platform.modules.circle.info.dao.CircleInfoDao;
import com.javakc.platform.modules.circle.info.entity.CircleInfo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

//圈子信息 逻辑                                                   接口，         实现类

@Service
@Cacheable
@CacheConfig(cacheNames = "circle:info")//加入缓存
public class CircleInfoService extends BaseService<CircleInfoDao, CircleInfo> {

@Transactional
//@CacheEvict(allEntries = true)//全清
//@CacheEvict(key="'page:1:10'")//
public void insert(CircleInfo entity){
    baseMapper.insert(entity);
}

//@CacheEvict(allEntries = true)
public void  update(CircleInfo entity){
    baseMapper.updateById(entity);
}

    /**
     * 按照条件分页查询
     * @param entity
     * @return
     *
     */
    @Cacheable(key = "'page:'+#entity.pageNo+':'+#entity.pageSize")
    public Page<CircleInfo> page(CircleInfo entity){

//        封装动态查询条件
        QueryWrapper<CircleInfo> wrapper =new QueryWrapper<>();

//        封装动态分页参数
    Page<CircleInfo> page= Page.of(entity.getPageNo(),entity.getPageSize());
//    判断数据是否存在
    if (StringUtils.hasLength(entity.getTitle()))
        wrapper.eq("title",entity.getTitle());
//        使用分页查询方法

        return baseMapper.selectPage(page,wrapper);

}

public CircleInfo queryById(int id){
        return  baseMapper.selectById(id);
}

}
